package com.meepo.basic.auth

import com.meepo.basic.schema.user.User
import com.meepo.sdk.utils.GsonUtils
import com.meepo.sdk.utils.SPUtils

object Store {

    const val RESEND_CODE_DURATION: Long = 60
    const val LOGIN_PASSWORD_MIN_LENGTH = 8

    const val SETTINGS_ITEM_SETTINGS = "settings"
    const val LOGIN_METHOD_CODE = "code"
    const val LOGIN_METHOD_PASSWORD = "password"
//    const val LOGIN_METHOD_ONE_KEY = "oneKey"

    const val USER_SUMMARY_KEY = "userSummaryKey"

    internal object State {
        var user: User? = null
    }

    object Func {
        fun isSelf(userId: String): Boolean {
            getUserSummary()?.id?.let {
                return it == userId
            }

            return false
        }

        fun saveUserSummary(user: User) {

            State.user = user
            SPUtils.getInstance().put(USER_SUMMARY_KEY, com.meepo.sdk.utils.GsonUtils.toJson(user))
//            SharedPrefsFactory.setUp(requireAppContext())
//                .setObject(USER_SUMMARY_KEY, user)
        }

        fun getUserSummary(): User? {

            var rsp: User? = State.user
            if (rsp == null) {
                SPUtils.getInstance().getString(USER_SUMMARY_KEY)?.let {
                    rsp = com.meepo.sdk.utils.GsonUtils.fromJson(it, User::class.java)
                }
                State.user = rsp
            }
            return rsp
        }

        fun clearUserSummary() {
            State.user = null

            SPUtils.getInstance().remove(USER_SUMMARY_KEY)
        }
    }
}